package vistas.paneles;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import vistas.abstractModels.AtletaTableModel;
import vistas.abstractModels.EntrenadorTableModel;
import vistas.abstractModels.EventoTableModel;
import vistas.abstractModels.MaterialTableModel;
import daos.AtletaDAO;
import daos.EntrenadorDAO;
import daos.EventoDAO;
import daos.MaterialDAO;
import entidades.Atleta;
import entidades.Entrenador;
import entidades.Evento;
import entidades.Material;

public class Consultas extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel top, bottom;
	private JButton entrenadores;
	private JButton atletas;
	private JButton material;
	private JButton eventos;
	// --------------------------------------- componente
	private JTable list;
	private JScrollPane listScroller, descripScroller;
	private JTextArea descripcion;
	// -------------------------------------- subcomponente botones
	private JPanel grupo;
	private JButton buscar, limpiar;
	private JTextField id;
	private EntrenadorDAO entrenadorDAO;
	private AtletaDAO atletaDAO;
	private MaterialDAO materialDAO;
	private EventoDAO eventoDAO;
	private Integer vista_actual;
	private Label label;
	
	
	public static final int ENTRENADOR = 0;
	public static final int ATLETA = 1;
	public static final int MATERIAL = 2;
	public static final int EVENTO = 3;

	public Consultas() {
		
		instancias();
		
		agregar_componentes();
		
		setListeners();
	}

	private void instancias() {
		entrenadorDAO = new EntrenadorDAO();
		atletaDAO = new AtletaDAO();
		eventoDAO = new EventoDAO();
		materialDAO = new MaterialDAO();
		
		top = new JPanel();
		bottom = new JPanel();
		entrenadores = new JButton("Entrenadores");
		atletas = new JButton("Atletas");
		material = new JButton("Material deportivo");
		eventos = new JButton("Eventos");
	
		list = new JTable();
		listScroller = new JScrollPane(list);
		
		descripcion = new JTextArea();
		descripScroller = new JScrollPane(descripcion);
		// ---------------- ^__^ -----------------------

		grupo = new JPanel();
		grupo.setPreferredSize(new Dimension(200, 400));
		id = new JTextField();
		buscar = new JButton("Buscar");
		limpiar = new JButton("Limpiar");
		id.setPreferredSize(new Dimension(400, 120));
		id.setBorder(BorderFactory.createTitledBorder("Buscar"));
		label = new Label("Seleccione una categoria");
		
	}
	
	private void agregar_componentes() {
		grupo.setLayout(new BoxLayout(grupo, BoxLayout.Y_AXIS));
		// top.setLayout(new BoxLayout(top, BoxLayout.X_AXIS));
		// top.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 20));
		bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));

		grupo.add(label);
		grupo.add(Box.createRigidArea(new Dimension(10, 10)));
		grupo.add(id);
		grupo.add(Box.createRigidArea(new Dimension(10, 10)));
		grupo.add(buscar);
		grupo.add(Box.createRigidArea(new Dimension(10, 10)));
		grupo.add(limpiar);

		top.setAlignmentX(LEFT_ALIGNMENT);
		top.add(entrenadores);
		top.add(atletas);
		top.add(material);
		top.add(eventos);

		bottom.add(listScroller);
		bottom.add(descripScroller);

		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		setLayout(gridbag);
		c.fill = GridBagConstraints.HORIZONTAL;

		// c.weighty = 0;//request any extra vertical space
		// c.weightx = 0;
		c.anchor = GridBagConstraints.WEST; // bottom of space
		c.gridwidth = 1;// columns wide
		c.gridy = 0;
		c.gridx = 0;
		c.insets = new Insets(10, 50, 10, 10);
		gridbag.setConstraints(top, c);
		add(top);

		c.weighty = 0;// request any extra vertical space
		c.anchor = GridBagConstraints.SOUTH; // bottom of space
		// c.gridwidth = 1;//2 columns wide
		c.gridy = 1;
		c.gridx = 1;
		c.insets = new Insets(10, 10, 15, 20);
		gridbag.setConstraints(grupo, c);
		add(grupo);

		c.weightx = 1.0;// request any extra vertical space
		c.weighty = 1.0;// request any extra vertical space
		// c.gridwidth = 4;//2 columns wide
		c.gridy = 1;
		c.gridx = 0;
		c.insets = new Insets(10, 50, 10, 10);
		c.ipady = 5000;
		c.ipadx = 300;
		gridbag.setConstraints(bottom, c);
		add(bottom);		
		
		descripcion.setEditable(false);
		descripcion.setBorder(BorderFactory.createTitledBorder("Descripción"));
	}

	void setListeners() {

		entrenadores.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				list.setModel(new EntrenadorTableModel(entrenadorDAO
						.obtenListaEntrenador()));
				listScroller.setBorder(BorderFactory
						.createTitledBorder("Entrenadores"));
				vista_actual = ENTRENADOR;
				label.setText("Buscar en Entrenadores");
				id.setBorder(BorderFactory.createTitledBorder("Nombre"));
			}
		});


		atletas.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				list.setModel(new AtletaTableModel(atletaDAO
						.obtenListaAtleta(), AtletaTableModel.FULL));
				listScroller.setBorder(BorderFactory
						.createTitledBorder("Atletas"));
				vista_actual = ATLETA;
				label.setText("Buscar en Atletas");
				id.setBorder(BorderFactory.createTitledBorder("Nombre"));

			}
		});

		material.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				list.setModel(new MaterialTableModel(materialDAO
						.obtenListaMaterial()));
				listScroller.setBorder(BorderFactory
						.createTitledBorder("Material"));
				vista_actual = MATERIAL;
				label.setText("Buscar en Material");
				id.setBorder(BorderFactory.createTitledBorder("Descripción"));
			}
		});
		
		eventos.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				list.setModel(new EventoTableModel(eventoDAO.obtenListaEventos()));
				listScroller.setBorder(BorderFactory
						.createTitledBorder("Eventos"));
				vista_actual = EVENTO;
				label.setText("Buscar en Eventos");
				id.setBorder(BorderFactory.createTitledBorder("Descripción"));
			}
		});
		
		list.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				
				String desc = new String();
			
				if (vista_actual == ENTRENADOR) {
					Integer identificador = Integer.parseInt((String) list
							.getValueAt(list.getSelectedRow(), 0));
					Entrenador entrenador = entrenadorDAO.obtenEntrenador(identificador);
					desc = entrenador.getId().toString() + "\n"
							+ entrenador.getNombres() + "\n" + entrenador.getApellidos()
							+ "\n" + entrenador.getDireccion() + "\n"
							+ entrenador.getTelefonoA() + "\n" + "Cinturón "
							+ entrenador.getCinturon() + "\n" + "Dan "
							+ entrenador.getDan();
				}

				if (vista_actual == ATLETA) {
					Integer identificador = Integer.parseInt((String) list
							.getValueAt(list.getSelectedRow(), 0));
					Atleta atleta = atletaDAO.obtenAtleta(identificador);
					desc = atleta.getId().toString() + "\n"
							+ atleta.getNombres() + "\n" + atleta.getApellidos()
							+ "\n" + atleta.getDireccion() + "\n"
							+ atleta.getTelefonoA() + "\n" + "Cinturón "
							+ atleta.getCinturon() + "\n" + "Deuda "
							+ atleta.getDeuda() + " Meses";
				}

				if (vista_actual == MATERIAL) {
					Integer identificador = Integer.parseInt((String) list
							.getValueAt(list.getSelectedRow(), 0));
					Material material = materialDAO.obtenMaterial(identificador);
					desc = String.valueOf(material.getId()) + "\n"
							+ material.getDescripcion() + "\n" 
							+ material.getTipo();
				}
				
				if (vista_actual == EVENTO) {
					Integer identificador = Integer.parseInt((String) list
							.getValueAt(list.getSelectedRow(), 0));
					Evento evento = eventoDAO.obtenEvento(identificador);
					desc = String.valueOf(evento.getId()) + "\n"
							+ evento.getDescripcion() + "\n" 
							+ evento.getNombre();
				}

				descripcion.setText(desc);

			}
		});

		buscar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				if (vista_actual == ATLETA) {
					list.setModel(new AtletaTableModel(atletaDAO
							.consultarAtleta(id.getText()), AtletaTableModel.FULL));
				}
				
				if (vista_actual == ENTRENADOR) {
					list.setModel(new EntrenadorTableModel(entrenadorDAO
							.consultarEntrenador(id.getText())));
				}
				
				if (vista_actual == MATERIAL) {
					list.setModel(new MaterialTableModel(materialDAO
							.consultarMaterial(id.getText())));
				}
				
				if (vista_actual == EVENTO) {
					list.setModel(new EventoTableModel(eventoDAO
							.consultarEvento(id.getText())));
				}
			}
		});
	}

}
